<?php
require_once __DIR__."/../vendor/autoload.php";

$app_id = '8000000';//开发者

$app_secret = '10bf3984d33900906df9fabdbfda59e8';//开发者

$device_id = trim($_POST['mkey']);

$device_secret = trim($_POST['msign']);

$rpc = new \zhongwu\protocol\RpcClient($app_id, $app_secret, 'http://api.zhongwuyun.com');

$Zprinter = new \zhongwu\Printer($rpc);


function cny($money) {

    $cnums=array("0,","1,","2,","3,","4,","5,","6,","7,","8,","9,");

    $cnyunits=array("点,","元");

    $grees=array("十,","百,","千,","万,","十,");

    if(strpos($money,'.') !== false){ //拆分整数小数 分别赋值给变量

        list($int,$dec)=explode(".",$money,2);

    }else{

        list($int,$dec)=[$money,0];

    }

    $dec=array_filter(array($dec)); //过滤分数

    $ret=array_merge($dec,array(implode("",cny_to_unit(str_split($int),$grees)),"")); //先处理单位

    $ret=implode("",array_reverse(cny_to_unit($ret,$cnyunits)));

    return str_replace(array_keys($cnums),$cnums,$ret);

}

function cny_to_unit($list,$units) {

    $ul=count($units);
    $count = count($list);

    $xs=array();

    foreach (array_reverse($list) as $x) { //反转数组便于处理

        $l=count($xs);

        if ($x!="0" || !($l%4)) {

            if ($l != 0) {

                if($ul==2)
                {
                    if (($count%2))
                    {
                        $n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]);
                    }
                    else
                    {
                        $n = ($x == '0' ? '' : $x) . ($units[($l) % $ul]);
                    }
                }
                else
                {
                    $n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]);
                }
            }else{

                $n=($x=='0'?'':$x);

            }

        }

        else {

            $n=is_numeric($xs[0][0])?$x:'';

        }

        array_unshift($xs,$n);

    }

    return $xs;

}


try {
    //$voice = json_encode(["收到会员充值","微信支付","两","万","4","千","0","2","十","元"]);
    $voicePostList = explode(",",trim($_POST['voice']));
    $voiceList = [];
    foreach ($voicePostList as $voice)
    {
        $money_reg = '/^[1-9]\d*|^[1-9]\d*.\d+[1-9]$/';
        if(preg_match($money_reg, $voice)){
            $moneyList=explode(",",cny($voice));
        }
        else
        {
            $voiceList[] = $voice;
        }
    }

    $voiceList = array_merge($voiceList,$moneyList);

    $voice = json_encode($voiceList);

    $rs = $Zprinter->set_args($device_id, $device_secret)->set_voice($voice);

    var_dump($rs);

} catch (Exception $e) {

}
